package main.java.ensao.cryptography.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigInteger;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import main.java.ensao.cryptography.rsamodule.PublicKey;
import main.java.ensao.cryptography.rsamodule.RSA;
import main.java.ensao.cryptography.rsamodule.SecretKey;

public class RSAPane extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	BigInteger calc_n;
	BigInteger calc_d;
	BigInteger out_chiffrement;
	BigInteger out_dechiffrement;
	BigInteger calc_e;
	RSA rsa = new RSA();

	JLabel l1, l2, l3, l4, l5;
	JTextArea result_dechiffrement;
	JTextArea result_chiffrement;
	JTextField p, q, n, ee, d;
	JTextArea txt, txt_chiffre;
	JPanel txt_chiffrer, txt_clair, result_clair;
	JButton calcul_n_generate_e, calcul_d, bnt_chiffre, btn_generate_pq,
			bnt_dechiffre;

	public RSAPane() {
		this.init();
	}

	public void init() {
		// un premier onglet
		// this.setLayout(new BorderLayout());

		JPanel p_q = new JPanel();
		p_q.setBorder(BorderFactory.createTitledBorder("Saisir deux entiers ou générer les :"));

		l1 = new JLabel("p : ");
		p = new JTextField(17);

		l2 = new JLabel("q : ");
		q = new JTextField(17);

		btn_generate_pq = new JButton("générer p,q");

		p_q.add(l1);
		p_q.add(p);
		p_q.add(l2);
		p_q.add(q);
		p_q.add(btn_generate_pq);

		JPanel cle_publique = new JPanel();
		cle_publique
				.setBorder(BorderFactory.createTitledBorder("Clé publique"));

		l3 = new JLabel("n : ");
		n = new JTextField(13);

		l4 = new JLabel("e : ");
		ee = new JTextField(13);

		calcul_n_generate_e = new JButton("calculer n, générer e");

		cle_publique.add(l3);
		cle_publique.add(n);
		cle_publique.add(l4);
		cle_publique.add(ee);
		cle_publique.add(calcul_n_generate_e);

		JPanel cle_secrete = new JPanel();
		cle_secrete.setBorder(BorderFactory.createTitledBorder("Clé secrète"));

		l5 = new JLabel("d : ");
		d = new JTextField(13);
		calcul_d = new JButton("calculer d");

		cle_secrete.add(l5);
		cle_secrete.add(d);
		cle_secrete.add(calcul_d);

		JPanel txt_clair = new JPanel();
		txt_clair.setBorder(BorderFactory.createTitledBorder("Texte clair "));

		txt = new JTextArea(5, 20);
		txt.setLineWrap(true);
		txt_clair.add(txt);

		bnt_chiffre = new JButton("  chiffrer  ");
		txt_chiffrer = new JPanel();
		txt_chiffrer.setEnabled(false);
		txt_chiffrer.setBorder(BorderFactory
				.createTitledBorder("Résultat du chiffrement "));

		result_chiffrement = new JTextArea(5, 20);
		result_chiffrement.setLineWrap(true);
		txt_chiffrer.add(result_chiffrement);
		txt_chiffrer.add(new JLabel("         "));
		txt_chiffrer.add(new JLabel("         "));
		txt_chiffrer.add(new JLabel("         "));
		txt_chiffrer.add(new JLabel("         "));
		txt_chiffrer.add(new JLabel("         "));
		txt_chiffrer.add(new JLabel("         "));

		JPanel chiffrement = new JPanel();
		chiffrement.setLayout(new BoxLayout(chiffrement, BoxLayout.LINE_AXIS));

		chiffrement.add(txt_clair);
		chiffrement.add(bnt_chiffre);
		chiffrement.add(txt_chiffrer);

		JPanel msg_chiffre = new JPanel();
		msg_chiffre
				.setBorder(BorderFactory.createTitledBorder("Texte chiffré"));
		txt_chiffre = new JTextArea(5, 20);
		
		msg_chiffre.add(txt_chiffre);
		txt_chiffre.setLineWrap(true);

		bnt_dechiffre = new JButton("déchiffrer");

		result_clair = new JPanel();
		result_clair.setEnabled(false);
		result_clair.setBorder(BorderFactory
				.createTitledBorder("Résultat du déchiffrement "));

		result_dechiffrement = new JTextArea(5, 20);
		result_dechiffrement.setLineWrap(true);
		result_clair.add(result_dechiffrement);
		result_clair.add(new JLabel("         "));
		result_clair.add(new JLabel("         "));
		result_clair.add(new JLabel("         "));
		result_clair.add(new JLabel("         "));
		result_clair.add(new JLabel("         "));
		result_clair.add(new JLabel("         "));

		JPanel dechiffrement = new JPanel();
		dechiffrement.setLayout(new BoxLayout(dechiffrement,
				BoxLayout.LINE_AXIS));

		dechiffrement.add(msg_chiffre);
		dechiffrement.add(bnt_dechiffre);
		dechiffrement.add(result_clair);

		// traitement

		btn_generate_pq.addActionListener(new RSACryptageListener());
		calcul_n_generate_e.addActionListener(new RSACryptageListener());
		calcul_d.addActionListener(new RSACryptageListener());
		bnt_chiffre.addActionListener(new RSACryptageListener());
		bnt_dechiffre.addActionListener(new RSACryptageListener());

		this.add(p_q);
		this.add(cle_publique);
		this.add(cle_secrete);
		this.add(chiffrement);
		this.add(dechiffrement);

	//	Color y = new Color(64, 98, 125);
	//	this.setBackground(y);

	}

	class RSACryptageListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {

			if (e.getSource() == btn_generate_pq) {

				p.setText(String.valueOf(PublicKey.calcul_pq()));
				q.setText(String.valueOf(PublicKey.calcul_pq()));

				p.revalidate();
				q.revalidate();
			}

			if (e.getSource() == calcul_n_generate_e) {

				calc_n = PublicKey.calcul_n(new BigInteger(p.getText()),
						new BigInteger(q.getText()));

				n.setText(String.valueOf(calc_n));
				n.revalidate();

				calc_e = PublicKey.generate_e(new BigInteger(p.getText()),
						new BigInteger(q.getText()));

				ee.setText(String.valueOf(calc_e));
				ee.revalidate();
				
				System.out.println("e.getText()  "+ee.getText());
				

			} else if (e.getSource() == calcul_d) {
				
				System.out.println("ppppp  "+p.getText());
				System.out.println("qqqqq  "+q.getText());

				calc_d = SecretKey.calcul_d(new BigInteger(ee.getText()),
						new BigInteger(p.getText()),
						new BigInteger(q.getText()));
				
				System.out.println("calcul de d "+calc_d);
				
				d.setText(String.valueOf(calc_d));
				d.revalidate();

			} else if (e.getSource() == bnt_chiffre) {

				
				out_chiffrement = rsa.cryptage(new BigInteger(n.getText()),
						new BigInteger(ee.getText()),
						new BigInteger(txt.getText()));
				txt.revalidate();
				System.out.println("new big "+new BigInteger(n.getText()));
				
				
				txt_chiffrer.setEnabled(true);
				result_chiffrement.setText(String.valueOf(out_chiffrement));
				result_chiffrement.revalidate();
				
			} else if (e.getSource() == bnt_dechiffre) {

				txt_chiffre.revalidate();
				
				System.out.println(txt_chiffre.getText());
				System.out.println(n.getText());
				System.out.println(d.getText());
				out_dechiffrement = rsa.decryptage(new BigInteger(n.getText()),
						
						new BigInteger(d.getText()),
						new BigInteger(txt_chiffre.getText()));
				
				
				
				result_clair.setEnabled(true);
				result_dechiffrement.setText(String.valueOf(out_dechiffrement));
				result_dechiffrement.revalidate();
			}

		}

	}

}